<template>
  <div>
    <div class="tab_wrap">
      <van-tabs v-model="active" @click="onClick">
        <van-tab title="正常"></van-tab>
        <van-tab title="作废"></van-tab>
      </van-tabs>
      <div class="filter">
        <img
          src="@/assets/img/icon/icon_search.png"
          alt="搜索"
          @click="search"
        />
      </div>
    </div>

    <div class="page_space">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <card-wrap
          v-for="(item, index) in list"
          :key="index"
          arrow
          class="mt-15"
          :title="item.sampleName"
          :number="item.code"
          @click="goto('/inspection-details?id=' + item.id)"
        >
          <card-item title="经营主体">{{
            item.billInfoDTO.intercourseEnterpriseName
          }}</card-item>
          <card-item title="所属市场">{{
            item.billInfoDTO.belongEntName
          }}</card-item>
          <card-item title="检测日期">{{
            item.billInfoDTO.billDate
          }}</card-item>
          <card-item title="检测结果">
            <span v-html="item.resultHtml"></span>
          </card-item>
          <template v-if="active === 0" #footer>
            <cardBtn
              bottomText="作废"
              bottomDanger
              @bottomClick="deleteInspection(item.id)"
            />
          </template>
        </card-wrap>

        <template v-if="active === 1"> </template>
      </van-list>
    </div>
  </div>
</template>

<script>
import cardWrap from '@/components/base-card/wrap'
import cardItem from '@/components/base-card/item'
import cardBtn from '@/components/base-card/btn'
import { getInspection, deleteInspection } from '@/api/inspection'
import { store, mutations } from '@/observable'
import { delKeepaliveCache } from '@/utils'

export default {
  name: 'inspectionLedger',
  components: { cardWrap, cardItem, cardBtn },
  data() {
    return {
      loading: false,
      finished: false, // 触底加载结束
      list: [],
      params: {
        state: 'NORMAL',
        limit: 10,
        page: 1
      },
      value: '', // 搜索
      active: 0 // tabs
    }
  },
  beforeRouteLeave(to, from, next) {
    // 返回 清除缓存
    if (to.name !== 'inspectionSearch') {
      mutations.setSearch({})
      delKeepaliveCache(this)
      console.log('销毁:', to.name)
    }
    next()
  },
  activated() {
    // 获取-搜索字
    if (JSON.stringify(store.search) !== '{}') {
      let info = store.search
      this.params = { ...this.params, ...info }
      this.getList(this.params, true)
    }
  },
  methods: {
    search() {
      if (JSON.stringify(store.search) !== '{}') {
        // 再次点击搜索则重置
        mutations.setSearch({})
        this.getList(this.params, true)
      } else {
        this.$router.push('/inspection-search')
      }
    },
    // 触底加载
    onLoad() {
      this.getList(this.params)
    },
    // 作废
    deleteInspection(id) {
      this.$dialog
        .alert({
          title: '是否作废?',
          showCancelButton: true
        })
        .then(async () => {
          const { code } = await deleteInspection(id)
          if (code === '0') {
            console.log('success')
            this.getList(this.params, true)
          } else {
            console.log('fail')
          }
        })
        .catch(() => {
          // on cancel
        })
    },
    // 切换tabs
    onClick(name) {
      let state = ''
      // 0 待检测 ToBeChecked 1 初检不合格 FirstUnqualified
      switch (name) {
        case 0:
          state = 'NORMAL'
          break
        case 1:
          state = 'CANCEL'
          break
      }
      this.params.state = state
      this.getList(this.params, true)
    },
    // 获取列表
    async getList(params, isReset) {
      if (isReset) {
        this.loading = true
        this.finished = false
        this.list = []
        this.params.page = 1
        // this.params = {
        //   state: this.active === 0 ? 'NORMAL' : 'CANCEL',
        //   limit: 10,
        //   page: 1
        // }
      }
      const { code, data } = await getInspection(params)
      if (code === '0') {
        data.content.forEach((item) => {
          this.list.push(item)
        })
        // 加载状态结束
        this.loading = false
        this.params.page++
        // 数据全部加载完成
        if (data.last) {
          this.finished = true
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
.tab_wrap {
  display: flex;
  background: #fff;
  position: sticky;
  top: 0;
  z-index: 999;

  .van-tabs {
    width: 100%;
  }

  .filter {
    width: 54px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    img {
      width: 20px;
      height: 20px;
    }
  }
}
</style>
